package com.pruebas.physics.common;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Cube{
	private float side;
	
	private float initialX;
	private float initialY;
	private float initialZ;
	
	private FloatBuffer floatBuffer;
	private ByteBuffer byteBuffer;
	
	public Cube(float side) {
		this(side, 0f, 0f, 0f);
	}
	
	public Cube(float side, float initialX, float initialY, float initialZ) {
		this.side = side;
		this.initialX = initialX;
		this.initialY = initialY;
		this.initialZ = initialZ;
		makefloatBuffer();
	}
	
	public float getSide() {
		return side;
	}
	
	public float getInitialX() {
		return initialX;
	}
	
	public float getInitialY() {
		return initialY;
	}
	
	public float getInitialZ() {
		return initialZ;
	}
	
	public void draw(GL10 gl){
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.floatBuffer);
		
		gl.glDrawElements(GL10.GL_TRIANGLE_FAN, 20, GL10.GL_UNSIGNED_BYTE, this.byteBuffer);
		//gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 8);
	}
	
	private void makefloatBuffer() {
		float halfSide = this.side / 2;
		float left = this.initialX - halfSide;
		float right = this.initialX + halfSide;
		float top = this.initialY + halfSide;
		float buttom = this.initialY - halfSide;
		float near = this.initialZ + halfSide;
		float far = this.initialZ - halfSide;
		
		float[] coords = {
				left, buttom, near,
				right, buttom, near,
				right, top, near,
				left, top, near,
				left, buttom, far,
				right, buttom, far,
				right, top, far,
				left, top, far
			
		};
		
		byte[] indexes = {
			//0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
			0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 5, 3, 1, 4, 3, 6, 2, 4, 0, 1 
		};
		
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(coords.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		this.floatBuffer = byteBuffer.asFloatBuffer();
		this.floatBuffer.put(coords);
		this.floatBuffer.position(0);
		
		this.byteBuffer = ByteBuffer.allocateDirect(indexes.length);
		this.byteBuffer.order(ByteOrder.nativeOrder());
		this.byteBuffer.put(indexes);
		this.byteBuffer.position(0);
	}
}
